﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockFinder.UI.Views;
using StockFinder.Model;
using StockFinder.UI.Controllers;
using StockFinder.DTO;

namespace StockFinder.Forms
{
    public partial class UrlListForm : Form, IUrlsListView
    {
        private readonly UrlsListViewController _Controller;

        public UrlListForm()
        {
            InitializeComponent();

            _Controller = new UrlsListViewController(this);
        }

        private void _StartButton_Click(object sender, EventArgs e)
        {
            //clear the datasource
            _UrlDataGridView.DataSource = null;
            _UrlDataGridView.Refresh();
            
            //start the download
            OnStart(); 
        }

        protected virtual void OnStart()
        {
            if (Start != null)
            {
                Start(this, EventArgs.Empty);
            }
        }

        protected virtual void OnInsertUrls()
        {
            if (InsertUrls != null)
            {
                InsertUrls(this, EventArgs.Empty);
            }
        }

        protected virtual void OnLoadFromDatabase()
        {
            if (LoadFromDatabase != null)
            {
                LoadFromDatabase(this, EventArgs.Empty);
            }
        }

        #region IUrlsListView Members

        public event EventHandler Start;
        public event EventHandler InsertUrls;
        public event EventHandler LoadFromDatabase;

        public List<IndexUrlDTO> GetSelectedIndexUrls()
        {
            List<IndexUrlDTO> indexUrlDTO = new List<IndexUrlDTO>();

            //loop parent datagrid

            //loop child datagrid

            return indexUrlDTO;
        }

        public void AppendLog(string logMessage)
        {
            //make sure on main thread
            Invoke(new MethodInvoker(delegate
            {
                //append to text box
                _UrlsLoggingRichTextBox.AppendText(logMessage);
                _UrlsLoggingRichTextBox.AppendText(Environment.NewLine);
                _UrlsLoggingRichTextBox.SelectionStart = _UrlsLoggingRichTextBox.Text.Length;
                _UrlsLoggingRichTextBox.ScrollToCaret();
            }));
        }

        public void SetIndexUrlsList(List<IndexUrlDTO> indexUrls)
        {
            //make sure on main thread
            Invoke(new MethodInvoker(delegate
            {
                _UrlDataGridView.DataSource = indexUrls;
            }));
        }

        #endregion

        private void _InsertButton_Click(object sender, EventArgs e)
        {
            OnInsertUrls();
        }

        private void _FromDatabaseButton_Click(object sender, EventArgs e)
        {
            //clear the datasource
            _UrlDataGridView.DataSource = null;
            _UrlDataGridView.Refresh();

            //alert to start download of new urls
            OnLoadFromDatabase();
        }

        private void UrlListForm_Load(object sender, EventArgs e)
        {
            OnLoadFromDatabase();
        }
    }
}
